<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.0.0-rc.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.0.0-rc.0" type="image/png" sizes="32x32"><meta name="description" content="冒泡排序是一个经典的算法类型，主要用于实现乱序元素到有序的排序。一般是从小到大或者从大到小的一个排序 本次我们以Java语言为例来实现冒泡排序">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-冒泡排序">
<meta property="og:url" content="https://tsing666.gitee.io/2022/01/25/Java-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/index.html">
<meta property="og:site_name" content="Tsing的博客">
<meta property="og:description" content="冒泡排序是一个经典的算法类型，主要用于实现乱序元素到有序的排序。一般是从小到大或者从大到小的一个排序 本次我们以Java语言为例来实现冒泡排序">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-25T12:57:12.000Z">
<meta property="article:modified_time" content="2022-01-25T13:41:50.298Z">
<meta property="article:author" content="Tsing的博客">
<meta property="article:tag" content="java">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary"><meta name="keywords" content="Tsing的博客, Tsing的博客"><meta name="description" content=""><title>Java-冒泡排序</title><link ref="canonical" href="https://tsing666.gitee.io/2022/01/25/Java-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.0.0-rc.0"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"carbon","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.0.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">Tsing的博客</div><div class="header-banner-info__subtitle">welcome to myblog</div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">Java-冒泡排序</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2022-01-25</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2022-01-25</span></span></div></header><div class="post-body"><p>冒泡排序是一个经典的算法类型，主要用于实现乱序元素到有序的排序。一般是从小到大或者从大到小的一个排序</p>
<p>本次我们以Java语言为例来实现冒泡排序</p>
<span id="more"></span>
<p>package test_1;<br>    //{10,5,60,15,90,85}<br>public class test_5 {<br>        public static void main(String[] args) {<br>            int [] arr ={10,5,60,15,90,85};//采用静态初始化初始一个乱序的数组<br>            //使用冒泡排序将其从大到小进行排序并输出结果<br>            for(int i=0;i&lt;arr.length-1;i++){//外层for循环控制比较次数 这里我们只比较5次 因为比到第四次的时候最小的那个值已经出来了<br>                for(int x=0;x&lt;arr.length-1;x++){//内层for循环用于遍历数组元素 并带到if语句中进行比较<br>                    if(arr[x]&lt;arr[x+1]){<br>                        int temp=arr[x];//使用一个int类型的变量暂存要被调换的元素<br>                        arr[x]=arr[x+1];//元素调换<br>                        arr[x+1]=temp;//将暂存的元素换到右边来<br>                    }<br>                }<br>            }<br>            //打印输出 因为数组不能直接以数组的形式即[元素1,元素2,…]打印出来 所以这里我手动添加了一个数组的样式<br>            //如果有学到能够用其它的方式将数组中的元素集合在一起以数组的形式打印出来的话就采取那种方法<br>            System.out.print(“[“);<br>            for(int i1=0;i1&lt;arr.length;i1++){<br>                if(i1==arr.length-1){<br>                    System.out.print(arr[i1]);<br>                }<br>                else{<br>                    System.out.print(arr[i1]+”,”);<br>                }<br>            }<br>            System.out.print(“]”);<br>        }<br>}</p>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://tsing666.gitee.io">Tsing的博客</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://tsing666.gitee.io/2022/01/25/Java-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/">https://tsing666.gitee.io/2022/01/25/Java-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://tsing666.gitee.io/tags/java/">java</a></span><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://tsing666.gitee.io/tags/%E7%AE%97%E6%B3%95/">算法</a></span></div><nav class="post-paginator paginator"></nav></footer></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/bgimg/博客头像.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">欢迎来到我的博客</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">1</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">1</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">2</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2022</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>Tsing的博客</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="/js/utils.js?v=2.0.0-rc.0"></script><script src="/js/stun-boot.js?v=2.0.0-rc.0"></script><script src="/js/scroll.js?v=2.0.0-rc.0"></script><script src="/js/header.js?v=2.0.0-rc.0"></script><script src="/js/sidebar.js?v=2.0.0-rc.0"></script></body></html>